package sparkcore.day7.lesson02

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.types.{IntegerType, StringType, StructField, StructType}
import org.apache.spark.sql.{DataFrame, Row, SQLContext}
import org.apache.spark.{SparkConf, SparkContext}

/**
  * Created by Administrator on 2018/5/3.
  */
object DataFrameProgrammatically {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local").setAppName("test")
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)
   val rdd = sc.textFile("D:\\1711班\\第十三天\\资料\\people.txt")
    val rowRDD: RDD[Row] = rdd.map(line => {
      val fields = line.split(",")
      Row(fields(0), fields(1).trim.toInt)
    })
    val structType = StructType(
      StructField("name", StringType, true) ::
        StructField("age", IntegerType, true) :: Nil
    )


    val schemaString = "name age"

    val fields = schemaString.split(" ")
      .map(fieldName => StructField(fieldName, StringType, nullable = true))
    val schema = StructType(fields)

    /**
      *
      * rowRDD : RDD[Row],
      * schema : StructType
      *
      */
    val df: DataFrame = sqlContext.createDataFrame( rowRDD,structType)
    df.createOrReplaceTempView("people")
    sqlContext.sql("select name from people").show()

  }

}
